<template>
  <el-card class="achievement-card" shadow="hover" :body-style="{ padding: '0' }">
    <div class="card-content">
      <!-- 成果封面图片 -->
      <div class="achievement-cover">
        <el-image
          :src="props.achievement.posterlink"
          fit="cover"
          class="cover-image"
          :preview-src-list="[props.achievement.posterlink]"
          :initial-index="0"
          preview-teleported
        >
          <template #error>
            <div class="image-placeholder">
              <el-icon class="placeholder-icon"><Picture /></el-icon>
              <span class="placeholder-text">暂无封面</span>
            </div>
          </template>
        </el-image>
        
        <!-- 悬浮操作按钮 -->
        <div class="cover-overlay">
          <el-button
            type="primary"
            :icon="View"
            circle
            size="large"
            @click="openAchievement"
            class="view-btn"
          />
        </div>
      </div>
      
      <!-- 成果信息 -->
      <div class="achievement-info">
        <!-- 用户头像和信息 -->
        <div class="user-section">
          <el-avatar
            :src="props.achievement.userInfo.avatar"
            :size="45"
            class="user-avatar"
            @click.stop="navigateToUser"
          >
            <el-icon><User /></el-icon>
          </el-avatar>
          
          <div class="user-details">
            <div class="user-name" @click.stop="navigateToUser">
              {{ props.achievement.userInfo.user.name }}
            </div>
            <div class="publish-date">
              <el-icon class="date-icon"><Calendar /></el-icon>
              <span>
                {{ formatDate(props.achievement.updatedAt || props.achievement.createdAt) }}
              </span>
            </div>
          </div>
        </div>
        
        <!-- 成果标题 -->
        <div class="achievement-title">
          <el-tooltip
            :content="props.achievement.title || '暂无标题'"
            placement="top"
            :disabled="!isTitleOverflow"
          >
            <h3 class="title-text" ref="titleRef">
              {{ props.achievement.title || '暂无标题' }}
            </h3>
          </el-tooltip>
        </div>
        
        <!-- 成果描述 -->
        <div class="achievement-description">
          <p class="description-text">
            {{ props.achievement.introduction || '暂无描述' }}
          </p>
        </div>
        
        <!-- 底部操作区 -->
        <div class="card-footer">
          <el-button
            type="primary"
            size="small"
            :icon="Link"
            @click="openAchievement"
            plain
          >
            查看详情
          </el-button>
          
          <div class="action-icons">
            <el-tooltip content="分享" placement="top">
              <el-button
                :icon="Share"
                circle
                size="small"
                @click="shareAchievement"
                text
              />
            </el-tooltip>
            
            <el-tooltip content="收藏" placement="top">
              <el-button
                :icon="Star"
                circle
                size="small"
                @click="favoriteAchievement"
                text
              />
            </el-tooltip>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script setup>
import { onMounted, ref, nextTick } from 'vue'
import dayjs from 'dayjs'
import { 
  Picture, 
  View, 
  User, 
  Calendar, 
  Link, 
  Share, 
  Star 
} from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const titleRef = ref(null)
const isTitleOverflow = ref(false)

const props = defineProps({
  achievement: {
    type: Object,
    required: true
  }
})

// 格式化日期
const formatDate = (date) => {
  return dayjs(date).format('YYYY-MM-DD')
}

// 导航到用户页面
const navigateToUser = () => {
  router.push({ 
    path: `/user/${props.achievement.userid}/article` 
  })
}

// 打开成果链接
const openAchievement = () => {
  const link = props.achievement.achievementlink || props.achievement.attachment
  if (link) {
    window.open(link, '_blank')
  } else {
    ElMessage.warning('暂无可查看的链接')
  }
}

// 分享成果
const shareAchievement = () => {
  const link = props.achievement.achievementlink || props.achievement.attachment
  if (link && navigator.share) {
    navigator.share({
      title: props.achievement.title,
      text: props.achievement.introduction,
      url: link
    }).catch(() => {
      copyToClipboard(link)
    })
  } else if (link) {
    copyToClipboard(link)
  } else {
    ElMessage.warning('暂无可分享的链接')
  }
}

// 复制到剪贴板
const copyToClipboard = (text) => {
  navigator.clipboard.writeText(text).then(() => {
    ElMessage.success('链接已复制到剪贴板')
  }).catch(() => {
    ElMessage.error('复制失败')
  })
}

// 收藏成果
const favoriteAchievement = () => {
  ElMessage.success('收藏功能待实现')
}

// 检查标题是否溢出
const checkTitleOverflow = () => {
  nextTick(() => {
    if (titleRef.value) {
      isTitleOverflow.value = titleRef.value.scrollWidth > titleRef.value.clientWidth
    }
  })
}

onMounted(() => {
  checkTitleOverflow()
})
</script>

<style scoped>
.achievement-card {
  width: 350px;
  height: 280px;
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
}

.achievement-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.card-content {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* 封面图片区域 */
.achievement-cover {
  position: relative;
  height: 140px;
  overflow: hidden;
}

.cover-image {
  width: 100%;
  height: 100%;
  transition: transform 0.3s ease;
}

.achievement-card:hover .cover-image {
  transform: scale(1.05);
}

.image-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  color: #909399;
}

.placeholder-icon {
  font-size: 32px;
  margin-bottom: 8px;
}

.placeholder-text {
  font-size: 12px;
}

/* 悬浮操作按钮 */
.cover-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.achievement-card:hover .cover-overlay {
  opacity: 1;
}

.view-btn {
  background: rgba(255, 255, 255, 0.9);
  border: none;
  color: #409eff;
}

.view-btn:hover {
  background: white;
  transform: scale(1.1);
}

/* 成果信息区域 */
.achievement-info {
  flex: 1;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* 用户信息区域 */
.user-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  cursor: pointer;
  transition: transform 0.2s ease;
  border: 2px solid #f0f0f0;
}

.user-avatar:hover {
  transform: scale(1.05);
  border-color: #409eff;
}

.user-details {
  flex: 1;
  min-width: 0;
}

.user-name {
  font-weight: 600;
  color: #303133;
  cursor: pointer;
  transition: color 0.2s ease;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-name:hover {
  color: #409eff;
}

.publish-date {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #909399;
  margin-top: 2px;
}

.date-icon {
  font-size: 12px;
}

/* 成果标题 */
.achievement-title {
  margin: 4px 0;
}

.title-text {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  transition: color 0.2s ease;
}

.title-text:hover {
  color: #409eff;
}

/* 成果描述 */
.achievement-description {
  flex: 1;
  overflow: hidden;
}

.description-text {
  font-size: 13px;
  color: #606266;
  line-height: 1.5;
  margin: 0;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 底部操作区 */
.card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 8px;
  border-top: 1px solid #f0f0f0;
}

.action-icons {
  display: flex;
  gap: 4px;
}

.action-icons .el-button {
  color: #909399;
  transition: all 0.2s ease;
}

.action-icons .el-button:hover {
  color: #409eff;
  transform: scale(1.1);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .achievement-card {
    width: 100%;
    max-width: 350px;
  }
  
  .card-footer {
    flex-direction: column;
    gap: 8px;
    align-items: stretch;
  }
  
  .action-icons {
    justify-content: center;
  }
}

/* 加载动画 */
@keyframes pulse {
  0% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
  100% {
    opacity: 1;
  }
}

.achievement-card.loading {
  animation: pulse 1.5s ease-in-out infinite;
}
</style>
